(function() {
	require(['jquery', 'box', 'form', 'bootstrapTable', 'bootstrapTableZh'],function(jquery, box) {
		/*禁止滚动条*/
		if($('li:first').hasClass('selected') == true){
			$('html').css('overflow','hidden');
			$('body').css('overflow','hidden');
		}else{
			$('html').css('overflow','scroll');
			$('body').css('overflow','hidden');
		}
		
		var $table = $('#notice_retrieve'),
			$addBtn = $('#notic_add'),
			formId = 'notice_create_form';
		
		function initTable() {//加载内容
			$table.bootstrapTable({
				width: '90%',
				columns: [
	      			{width: '25%', field: 'noticeName', title: '公告主题', align: 'center'},
	      			{width: '25%', field: 'content', title: '公告内容', align: 'center'},
	      			{width: '20%', field: 'publishTime', title: '发布时间', align: 'center'},
	      			{width: '10%', field: 'attachmentUrl', title: '附件', align: 'center', formatter: fundTable},
	      			{width: '5%', field: 'clickNum', title: '点击量', align: 'center'},
	      			/*{width: '5%', field: 'downNum', title: '下载量', align: 'center'},*/
	      			{width: '10%', field: 'crud', title: '操作', align: 'center', events: orgEvents, formatter: operateFormatter}]
	        });
	        
			setTimeout(function () {
	            $table.bootstrapTable('resetView');
	        }, 200);
	        
	        $table.on('expand-row.bs.table', function (e, index, row, $detail) {
	        	var detail = detailFormatter(index, row);
	        	$detail.html(detail);
	        });
	        $table.on('all.bs.table', function (e, name, args) {
	            console.log(name, args);
	        });
	    }
		
		function getIdSelections() {
	        return $.map($table.bootstrapTable('getSelections'), function (row) {
	            return row.id;
	        });
	    }
	    function responseHandler(res) {
	        $.each(res.rows, function (i, row) {
	            row.state = $.inArray(row.id, selections) !== -1;
	        });
	        return res;
	    }
	    
	    function fundTable(value, row, index) {
	    	if(row.attachmentUrl == null || row.attachmentUrl == ""){
	    		return '暂无附件'
	    	}else{
	    		return '<a href="'+ row.attachmentUrl +'" target="view_window">查看</a>';
	    	}
	    }
	    
	    /*table中  “+” 按钮添加内容*/
	    function detailFormatter(index, row) {
	        var html = [], len = row.organizationNoticeRefList.length, divLi = "";
	        if(len != 0){
	        	for(var i = 0; i < len; i++){
		        	divLi += '<li style="margin: 3px 5px;float: left;font-size: 16px;border: 2px solid #8ca8d2;border-style: dashed;border-radius: 5px;padding: 3px 10px;line-height: 25px;">'+ 
		        			row.organizationNoticeRefList[i].orgName +'</li>';
		        }
	            html.push('<ul>'+ divLi +'</ul>');
	        }else{
	        	html.push('<p style="font-size: 16px;">暂无查看信息</p>')
	        }
	        return html.join('');
	    }
	    
	    function operateFormatter(value, row, index) {
	    	
	        return [
                '<a class="check" href="javascript:void(0)" title="查看详情">',
	            '<i class="glyphicon glyphicon-eye-open"></i>',
	            '</a>  ',
	            '<a class="upload" href="javascript:void(0)" title="上传文件">',
	            '<i class="glyphicon glyphicon-arrow-up"></i>',
	            '</a>  ',
	        ].join('');
	    }
	    
	    window.orgEvents = {
	        'click .check': function (e, value, row, index) {
				var modalObj = box.detailModal('subContent', "详情查看"),
				modalId = modalObj.modalId, modalBodyId = modalObj.modalBodyId, btnSure = modalObj.btnSure;
				var detailUrl = "system/getPage.service?path=businessManage/notice/noticeDetail",
				_target = modalBodyId, _selector= "#notice_detail";
				orange.loadPage({url: detailUrl, target: _target, selector: _selector, success: function(response){
					var modal = $('#' + modalId), 
					btn_sure = $('#' + btnSure);
					$('#subContent').data('row', row);
					modal.modal({backdrop: "static"});
					//判断是否有附件
					var detailModal = '';
					if(row.attachmentUrl == null || row.attachmentUrl == ""){
						detailModal = '<strong>'+ row.noticeName +'</strong>'+
						'<p style = "border: 1px solid #aaa;border-radius: 5px;padding: 5px;min-height: 200px;margin-top: 20px;">'+row.content+'</p>'+
				        '<p style="height: 50px;margin-top: 15px;">附件：无';
					}else{
						detailModal = '<strong>'+ row.noticeName +'</strong>'+
						'<p style = "border: 1px solid #aaa;border-radius: 5px;padding: 5px;min-height: 200px;margin-top: 20px;">'+row.content+'</p>'+
				        '<p style="height: 50px;margin-top: 15px;">附件：'+
				        '<a href="'+ row.attachmentUrl +'" target="view_window">'+ row.noticeName +'</a></p>';
					}
					
					$('#notice_detail').append(detailModal);
					
					btn_sure.unbind('click').bind('click', function() {
						modal.modal('hide');
					});
				}});
	        },
	        'click .upload': function (e, value, row, index) {
				//弹出模态框
		    	if($('#uploadPage_form').length<1) {
	        		var createModal = _createModal();
		        	$('#subContent').append(createModal);
	        	}
		    	var createUrl = "system/getPage.service?path=businessManage/fileUpload",
	        		_target = "file_modal_body", _selector= "#uploadPage_form";
	        	orange.loadPage({url: createUrl, target: _target, selector: _selector, success: function(response){
	        		var modal = $('#file-create-modal'),
	        			btn_sure = $('#btn_file_sure');
	        		$('#subContent').data('row', row);
	    			modal.modal({backdrop: "static"});
	    			btn_sure.unbind('click').bind('click', function() {
	    				modal.modal('hide');
	    				ajaxRequest();
	    			});
	        	}});
	        }
	    };
	    
	    //上传文件模态框
	    function _createModal() {
			var createModal = '<div class="modal fade" id="file-create-modal" tabindex="-1">' +
				'<div class="modal-dialog">' +
					'<div class="modal-content small-box">' +
						'<div class="modal-header">' +
							'<button type="button" class="close" data-dismiss="modal">&times;</button>' +
							'<h3 class="modal-title">上传附件</h3>' +
						'</div>' +
						'<div class="modal-body col-md-12" id="file_modal_body"></div>' +
						'<div class="modal-footer">' +
						  	'<button type="button" class="btn btn-sm" id="btn_file_sure">关闭</button>' +
						'</div>' +
					'</div>' +
				'</div>' +
			'</div>';
			return createModal;
		}
	    
	    //加载全部数据
	    function ajaxRequest() {
	    	$.ajax({
				url:"notice/retieveNotices.service",
				type: 'post',
				success: function(data) {
					$table.bootstrapTable('load', data.data);
				},
				complete: function() {
				}
			});
	    }
			
		//添加
		$addBtn.click(function () {
	    	//弹出模态框
	    	var modalObj = box.loadCreateModal('subContent', "创建公告"),
	    		modalId = modalObj.modalId, modalBodyId = modalObj.modalBodyId, btnSure = modalObj.btnSure;
	    	
	    	var createUrl = "system/getPage.service?path=businessManage/notice/noticeCreate",
        		_target = modalBodyId, _selector= "#notice_create";
	    	
        	orange.loadPage({url: createUrl, target: _target, selector: _selector, success: function(response){
        		var modal = $('#' + modalId);
    			modal.modal({backdrop: "static"});
    			var btn_sure = $('#' + btnSure);
    			
    			btn_sure.unbind('click').bind('click', function(btn) {
    				/*整体页面校验*/
					$('#notice_create_form').data('bootstrapValidator').validate();
					var jsonData = {}, flag = $('#notice_create_form').data('bootstrapValidator').isValid();
					if(flag) {
						btn_sure.attr("disabled", true);
	    				jsonData = $('#notice_create_form').form('getFormData', {id:formId});
	    				jsonData['noticeObject'] = $('#create_noticeObject').val().toString();
	    				$.ajax({
	    					url: 'notice/createNotice.service',
	    					type: 'post',
	    					data: jsonData,
	    					success : function(data) {
	    						var modalObj = box.tipModal(''),
	    			        		modalId = modalObj.modalId, modalBodyId = modalObj.modalBodyId;
	    			        	var tip_modal = $('#' + modalId);
	    						if(data && data.success) {
	    							$table.bootstrapTable('prepend', data.data);
	    							ajaxRequest();
	        	    	            modal.modal('hide');
	    						}else {
	    							modal.modal('hide');
	    				        	$('#' + modalBodyId).html('公告添加失败。。。。');
	    				        	tip_modal.modal();
	    						}
	    					},
	    					error: function() {
	    						modal.modal('hide');
	    						var modalObj = box.tipModal('subContent'),
	    			        	modalId = modalObj.modalId, modalBodyId = modalObj.modalBodyId;
	    			        	var tip_modal = $('#' + modalId);
					        	$('#' + modalBodyId).html('公告添加失败，网络异常。。。。');
				        		tip_modal.modal();
	    					},
	    					complete: function() {
	    						btn_sure.attr("disabled", false);
	    					}
	    				});
					}
    			});
        	}});
	    });
		
		/*//卡片上传文件按钮
		$('#file_info').click(function (item) {
			//弹出模态框
	    	if($('#uploadPage_form').length<1) {
        		var createModal = _createModal();
	        	$('#subContent').append(createModal);
        	}
	    	var createUrl = "system/getPage.service?path=businessManage/fileUpload",
        		_target = "file_modal_body", _selector= "#uploadPage_form";
        	orange.loadPage({url: createUrl, target: _target, selector: _selector, success: function(response){
        		var modal = $('#file-create-modal'),
        			btn_sure = $('#btn_file_sure');
        		//$('#subContent').data('row', row);
    			modal.modal({backdrop: "static"});
    			btn_sure.unbind('click').bind('click', function() {
    				modal.modal('hide');
    				ajaxRequest();
    			});
        	}});
		});*/
		
	 initTable();
	 ajaxRequest();
	    
	});
})();
